package com.chinapost.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.service.DeptServiceI;
import com.chinapost.service.WdServiceI;
import com.chinapost.vo.Json;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Wd;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "wdAction", results = { 
		@Result(name = "wdManager", location = "/jsp/wd/wdManager.jsp"),
		@Result(name = "wdAdd", location = "/jsp/wd/wdAdd.jsp"), 
		@Result(name = "wdEdit", location = "/jsp/wd/wdEdit.jsp") 
		})
public class WdAction extends BaseAction implements ModelDriven<Wd>, ServletRequestAware {

	private Wd wd = new Wd();
	private HttpServletRequest request;
	@Autowired
	private WdServiceI wdService;
	@Autowired
	private DeptServiceI deptService;


	@Override
	public Wd getModel() {
		return wd;
	}

	public String wdManager() {
		return "wdManager";
	}

	public String wdEdit() {
		return "wdEdit";
	}

	public String wdAdd() {
		return "wdAdd";
	}


	public void add() {
		Json j = new Json();
		try {
			wdService.add(wd);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}


	public void edit() {
		Json j = new Json();
		try {
			wdService.edit(wd);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		try {
			wdService.delete(wd);
			j.setSuccess(true);
			j.setMsg("删除成功！");
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	public void datagrid() {
		if (!StringUtils.isEmpty(wd.getDeptid())) {
			wd.setDeptIds(deptService.getChildIds(wd.getDeptid()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			wd.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(wdService.datagrid(wd));
	}
	

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
